﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace TRIG_MOD_DASHBOARDS
{
    class CommonMethods
    {
        public static DataTable CreateDataColumns(params string[] colna)
        {
            DataTable dt = new DataTable();
            foreach (string item in colna)
            {
                dt.Columns.Add(item);
            }
            //for (int i = 0; i < colna.Length; i++)
            //{
            //    if (i <= 1)
            //    {
            //        dt.Columns.Add(colna[i], typeof(string));
            //    }
            //    else
            //    {
            //        dt.Columns.Add(colna[i],typeof(string));
            //    }

            //}
            return dt;
        }

        public static void CreateLinkButton(string LinkText, string OnClickScript, GridViewRowEventArgs e, int ColumnNumber, bool EnableButton)
        {
            LinkButton lb = new LinkButton();
            lb.Text = LinkText;
            if (OnClickScript.StartsWith("javascript:"))
                lb.OnClientClick = OnClickScript;
            else
                //lb.PostBackUrl = OnClickScript;
                //lb.OnClientClick = "<script language=javascript>window.location.reload( false );</script>";
                lb.Attributes.Add("onclick", "loadFile(" + OnClickScript + ");");

            lb.Enabled = EnableButton;
            e.Row.Cells[ColumnNumber].Controls.Add((Control)lb);
        }

        public static DataTable GetItemsOfSpecificTypeFromWeb(string camQ, SPListTemplateType ListTemplateType)
        {
            using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    SPSiteDataQuery sQuery;
                    sQuery = new SPSiteDataQuery();
                    if (ListTemplateType.ToString() == "Tasks")
                        sQuery.Lists = "<Lists ServerTemplate='107' />"; //SPListTemplateType.Tasks
                    else
                        sQuery.Query = camQ;
                    return oWeb.GetSiteData(sQuery);
                }
            }
        }



        public static DataTable GetItemsOfSpecificTypeFromWeb(Guid SiteGid, Guid WebGid, string camQ, SPListTemplateType ListTemplateType)
        {
            SPWeb oWeb = new SPSite(SiteGid).OpenWeb(WebGid);
            SPSiteDataQuery sQuery;
            sQuery = new SPSiteDataQuery();
            if (ListTemplateType.ToString() == "Tasks")

                sQuery.Lists = "<Lists ServerTemplate='107' />"; //SPListTemplateType.Tasks
            else if (ListTemplateType.ToString() == "XMLForm")
                sQuery.Lists = "<Lists ServerTemplate='115' />";
            sQuery.Query = camQ;
            return oWeb.GetSiteData(sQuery);
        }

        //private void GetUserIfUserExists()
        //{
        //    using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
        //    {
        //        using (SPWeb oWeb = oSite.OpenWeb())
        //        {
        //            SPQuery qry = new SPQuery();
        //            qry.Query = "<Where><Eq><FieldRef Name=\"UserName\" /><Value Type=\"User\">" + SPContext.Current.Web.CurrentUser.LoginName + "</Value></Eq></Where>";
        //            SPListItem oListItem = oSite.OpenWeb().Lists.TryGetList("Users Master").GetItems(qry)[0];

        //            SPFieldUserValue userName = new SPFieldUserValue(oWeb,oListItem["UserName"].ToString());

        //            uProf.UserName = Convert.ToString(userName.User);

        //        }
        //    }

        //}
       
    }
}
